//
//  HYFVisitorView.swift
//  MyWeibo
//
//  Created by 黄勇锋 on 16/7/19.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//

import UIKit
import SnapKit

class HYFVisitorView: UIView {
    
    //声明一个 闭包
    var closure:(()->())?

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //写一个方法,供外部调用
    func setupVisitorViewInfo(imgName:String?,title:String?){
        
        if let img = imgName,tit = title {
            iv_house.image = UIImage(named: img)
            lb_message.text = tit
            iv_smallicon.hidden = true
        }else {
            
            //代表首页
            setupAnim()
        }
    }
    
    func setupAnim(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = M_PI * 2
        anim.duration = 10
        anim.repeatCount = MAXFLOAT
        //当切换控制器或者后台运行后,回到首页,里面的动画依然在进行
        anim.removedOnCompletion = false
        //添加动画
        iv_smallicon.layer.addAnimation(anim, forKey: nil)
    }
    
    
    func setupUI(){
        
        backgroundColor = UIColor(red: 237/255, green: 237/255, blue: 237/255, alpha: 1)
        //添加控件
        addSubview(iv_smallicon)
        addSubview(iv_mask)
        addSubview(iv_house)
        addSubview(lb_message)
        addSubview(btn_login)
        addSubview(btn_resign)
        
        //添加约束
        iv_smallicon.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        iv_house.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        iv_mask.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        
        lb_message.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(self)
            make.width.equalTo(230)
            make.height.equalTo(36)
            make.top.equalTo(iv_mask.snp_bottom).offset(16)
        }
        
        btn_login.snp_makeConstraints { (make) in
            
            make.top.equalTo(lb_message.snp_bottom).offset(10)
            make.size.equalTo(CGSize.init(width: 100, height: 36))
            make.leading.equalTo(lb_message)
        }
        
        btn_resign.snp_makeConstraints { (make) in
            
            make.size.equalTo(CGSize.init(width: 100, height: 36))
            make.top.equalTo(btn_login)
            make.trailing.equalTo(lb_message)
        }
        
        
    }
    
    //懒加载控件
    private lazy var iv_smallicon:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
    private lazy var iv_house:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //visitordiscover_feed_mask_smallicon
    private lazy var iv_mask:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //文字 label
    private lazy var lb_message:UILabel = {
        
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜 关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        label.textColor = UIColor.grayColor()
        
        return label
    }()
    
    //监听按钮的点击
    func buttonClick(){
        //调用闭包
        closure?()
    }
    
    private lazy var btn_login:UIButton = {
        
        let login = UIButton()
        login.setTitle("登陆", forState: UIControlState.Normal)
        login.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        login.titleLabel?.font = UIFont.systemFontOfSize(14)
        login.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        //添加点击事件
        login.addTarget(self, action: #selector(HYFVisitorView.buttonClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        return login
    }()
    
    private lazy var btn_resign:UIButton = {
        
        let login = UIButton()
        login.setTitle("注册", forState: UIControlState.Normal)
        login.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        login.titleLabel?.font = UIFont.systemFontOfSize(14)
        login.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        //添加点击事件
        login.addTarget(self, action: #selector(HYFVisitorView.buttonClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        return login
    }()
    
}
